home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / HTSort.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  1.0 KB  |  69 lines

  1. /* Simple string sorting support, thanks to qsort(). */
  2.  
  3. #include "HTUtils.h"
  4. #include <string.h>
  5.  
  6. #define SIZE_OF_HUNK 100
  7.  
  8. static char **hunk = NULL;
  9. static int size_of_hunk;
  10. static int count;
  11.  
  12. void HTSortInit (void)
  13. {
  14.   count = 0;
  15.  
  16.   if (!hunk)
  17.     {
  18.       size_of_hunk = SIZE_OF_HUNK;
  19.       hunk = (char **)malloc (sizeof (char *) * size_of_hunk);
  20.     }
  21.     
  22.   return;
  23. }
  24.  
  25. static void expand_hunk (void)
  26. {
  27.   /* Make hunk bigger by SIZE_OF_HUNK elements. */
  28.   size_of_hunk += SIZE_OF_HUNK;
  29.   hunk = (char **)realloc (hunk, sizeof (char *) * size_of_hunk);
  30.  
  31.   return;
  32. }
  33.  
  34. void HTSortAdd (char *str)
  35. {
  36.   /* If we don't have room, expand. */
  37.   if (count == size_of_hunk)
  38.     expand_hunk ();
  39.  
  40.   hunk[count++] = str;
  41.   
  42.   return;
  43. }
  44.  
  45. static int dsortf (char **s1, char **s2)
  46. {
  47.   return (strcmp (*(char **)s1, *(char **)s2));
  48. }
  49.  
  50. void HTSortSort (void)
  51. {
  52.   qsort ((void *)hunk, count, sizeof (char *), dsortf);
  53.  
  54.   return;
  55. }
  56.  
  57. int HTSortCurrentCount (void)
  58. {
  59.   return count;
  60. }
  61.  
  62. char *HTSortFetch (int i)
  63. {
  64.   if (i < count)
  65.     return hunk[i];
  66.   else
  67.     return NULL;
  68. }
  69.